#include<iostream>
#include<string>
using namespace std;

class Solution {
public:
    string reverseWords(string s) {
        int flag = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (isspace(s[i]))
            {
                reverse(s.begin() + flag, s.begin() + i);
                flag = i + 1;
            }
        }

        int pos = s.rfind(' ');
        reverse(s.begin() + pos + 1, s.end());

        return s;
    }
};